package de.tum.in.eist.poll.client;

import java.util.ArrayList;

import org.junit.AfterClass;
import org.junit.BeforeClass;

import com.google.gwt.core.client.GWT;
import com.google.gwt.junit.client.GWTTestCase;
import com.google.gwt.user.client.rpc.AsyncCallback;

import de.tum.in.eist.poll.client.Exceptions.*;
import de.tum.in.eist.poll.client.Model.*;

public class EIST_POLLTest extends GWTTestCase {
	private static PollServiceAsync pollSvc;
	private static Lecture tempLect;
	private static Feedback tempFeedback;
	
	@BeforeClass
	public static void setUpBeforeClass() throws Exception {
		// Callback object
//		pollSvc = GWT.create(PollService.class);
	}

	@AfterClass
	public static void tearDownAfterClass() throws Exception {
	}

	@Override
	public String getModuleName() {
		return "de.tum.in.eist.poll.EIST_POLL";
	}

	public void testCheckCredential() {
		System.out.println("test");
		if (pollSvc == null) {
			pollSvc = GWT.create(PollService.class);
		}
		
		AsyncCallback<Boolean> callback = new AsyncCallback<Boolean>() {

			
			public void onFailure(Throwable caught) {
				System.out.println("fail: " + caught.getMessage());
			}

			
			public void onSuccess(Boolean b) {
				System.out.println("win");
				assertEquals(false, b.booleanValue());
			}
			
		};
		
		String user = "admin";
		String password = "pw";
		pollSvc.checkCredentials(user, password, callback);
	}
	
	public void testAddLecture() {
		if (pollSvc == null) {
			pollSvc = GWT.create(PollService.class);
		}
		
		// Insert new Lect obj
		String subjectStr = "subject" + (Math.random());
		String lectStr = "lect" + (Math.random());
		Lecture l = new Lecture(subjectStr, lectStr, true);
		tempLect = l;
		
		AsyncCallback<Void> addLectCb = new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(Void r) {
				
			}
			
		};
		pollSvc.addLecture(l, addLectCb);
		
		AsyncCallback<Lecture[]> getLectCb = new AsyncCallback<Lecture[]>() {

			@Override
			public void onFailure(Throwable caught) {
				// TODO Auto-generated method stub
				
			}

			@Override
			public void onSuccess(Lecture[] result) {
				ArrayList<Lecture> lectList = new ArrayList<Lecture>();
				
				for (int i = 0; i < result.length; i++) {
					lectList.add(result[i]);
				}
				
				assertEquals(true, lectList.contains(tempLect));
			}
			
		};
		pollSvc.getLectures(getLectCb);
		
		// Test with null lect obj
		addLectCb = new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				
			}

			@Override
			public void onSuccess(Void r) {
				
			}
			
		};

		try{
			pollSvc.addLecture(null, addLectCb);
		} catch (Exception e) {
			System.out.println("exc");
			
			assertEquals(true, e instanceof ArgumentIsNullException);
		}

		
		// Test with lect object with invalid char
		addLectCb = new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				assertEquals(true, caught instanceof InvalidCharacterInputException);
			}

			@Override
			public void onSuccess(Void r) {
				
			}
			
		};
		Lecture invalLect = new Lecture("subject\\", "lect;", true);
		try {
			pollSvc.addLecture(invalLect, addLectCb);
		} catch (Exception e) {
			assertEquals(true, e instanceof InvalidCharacterInputException);
		}
	}
	
	public void testPushFeedback() {
		if (pollSvc == null) {
			pollSvc = GWT.create(PollService.class);
		}
		
		Lecture l = new Lecture("test", "test", true);
		AsyncCallback<Void> insertCb = new AsyncCallback<Void>() {

			@Override
			public void onFailure(Throwable caught) {
				
				
			}

			@Override
			public void onSuccess(Void result) {
				
				
			}
			
		};
		pollSvc.addLecture(l, insertCb);
		
		// Test pushFeedback with valid Feedback
		Feedback newFb = new Feedback((int)(Math.random()*5), "like" + Math.random(), 
				"dislike" + Math.random(), l.getEncodedKey());
		
		AsyncCallback<Boolean> pushCb = new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				
				
			}

			@Override
			public void onSuccess(Boolean result) {
				assertEquals(true, result.booleanValue());
				
			}
			
		};
		pollSvc.pushFeedbackForLecture(l, newFb, pushCb);
		
		// Test pushFeedback with null Lecture
		pushCb = new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				assertEquals(true, caught instanceof ArgumentIsNullException);
				
			}

			@Override
			public void onSuccess(Boolean result) {
				
			}
			
		};
		pollSvc.pushFeedbackForLecture(null, newFb, pushCb);
		
		// Test pushFeedback with feedback with invalid input
		pushCb = new AsyncCallback<Boolean>(){

			@Override
			public void onFailure(Throwable caught) {
				assertEquals(true, caught instanceof InvalidCharacterInputException);
				
			}

			@Override
			public void onSuccess(Boolean result) {
				
			}
			
		};
		pollSvc.pushFeedbackForLecture(null, newFb, pushCb);
	}
	
	public void deleteLectureTest() {
		if (pollSvc == null) {
			pollSvc = GWT.create(PollService.class);
		}
	}
}
